package com.longfei.admin.alarm_text;

import android.graphics.Bitmap;
import android.util.Log;

import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;


import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

/**
 * Created by admin on 2016/5/29.
 */
public class FaceppDetect {

    public interface CallBack{
        void success(JSONObject result);

        void error(FaceppParseException exception);
    }

    public static  void detect(final Bitmap bm,final CallBack callback){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //request
                    HttpRequests requests=new HttpRequests(Constant.Key,Constant.Secret,true,true);

                    Bitmap bmSmall=Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight());

                    ByteArrayOutputStream stream=new ByteArrayOutputStream();

                    bmSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);

                    byte[] bytes=stream.toByteArray();

                    PostParameters parameters=new PostParameters();
                    parameters.setImg(bytes);

                    JSONObject jsonObject = requests.detectionDetect(parameters);

                    //查看返回字符串，后面需解析
                    Log.e("TAG", jsonObject.toString());

                    if(callback!=null){
                        callback.success(jsonObject);
                    }

                } catch (FaceppParseException e) {
                    e.printStackTrace();

                    if(callback!=null){
                        callback.error(e);
                    }
                }
            }
        }).start();


    }

}
